package ercahshu

import "math"

func constructMaximumBinaryTree(nums []int) *TreeNode {
	if len(nums) == 0 {
		return nil
	}
	gen := getMax(nums)
	root := &TreeNode{Val: nums[gen]}
	root.Left = constructMaximumBinaryTree(nums[:gen])
	root.Right = constructMaximumBinaryTree(nums[gen+1:])
	return root
}

func getMax(nums []int) int {
	ans := -1
	cur := math.MinInt32
	for i, v := range nums {
		if cur < v {
			cur = v
			ans = i
		}
	}
	return ans
}
